﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using System.Data.Sql;
using Microsoft.Win32;

namespace DatabaseSelectorControlLibrary
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class DatabaseSelector : UserControl
    {
        private string dbConnectionString = null;
        private SqlConnection dbConnection = null;

        public DatabaseSelector()
        {
            InitializeComponent();
        }

        private void btnLoadServer_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataTable servers = SqlDataSourceEnumerator.Instance.GetDataSources();
                for (int i = 0; i < servers.Rows.Count; i++)
                {
                    cbxServers.Items.Add(servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
                    //comboDB2Ser.Items.Add(servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
                }
                //comboServers.SelectedIndex = 0;
                //comboDB2Ser.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnLoadDatabase_Click(object sender, RoutedEventArgs e)
        {
            Login dlg = new Login();
            if (dlg.ShowDialog() == true)
            {
                try
                {
                    dbConnectionString = "Data Source=" + cbxServers.Text + ";";
                    if (dlg.IsWindowsAuthentication)
                        dbConnectionString = dbConnectionString + " Integrated Security=true;";
                    else
                        dbConnectionString = dbConnectionString + " User ID=" + dlg.Username + "; Password=" + dlg.Password + ";";


                    dbConnection = new SqlConnection(dbConnectionString);
                    dbConnection.Open();
                    DataTable database = dbConnection.GetSchema("Databases");

                    cbxDatabases.ItemsSource = database.AsEnumerable();
                    cbxDatabases.DisplayMemberPath = "[Database_Name]";
                    cbxDatabases.SelectedIndex = 0;
                    dbConnection.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Please provide username and password.", "Error");
            }
        }

        public string DBConnectionString
        {
            get
            {
                if (dbConnectionString != null)
                    return dbConnectionString + " Initial Catalog=" + cbxDatabases.Text;
                else
                    throw new Exception("Servers OR Databases might not be loaded yet.");
            }
        }

        public string MainLabel
        {
            get
            {
                return grbServer.Header.ToString();
            }
            set
            {
                grbServer.Header = value;
            }
        }

        public string ServersLabel
        {
            get
            {
                return lblServerName.Content.ToString();
            }
            set
            {
                lblServerName.Content = value;
            }
        }

        public string DatabasesLabel
        {
            get
            {
                return lblDatabase.Content.ToString();
            }
            set
            {
                lblDatabase.Content = value;
            }
        }

        public string SelectedServer
        {
            get
            {
                return cbxServers.Text;
            }
        }

        public string SelectedDatabase
        {
            get
            {
                return cbxDatabases.Text;
            }
        }

    }
}
